package com.swm.util;

import com.swm.mapper.StockMapper;
import com.swm.pojo.Stock;
import org.apache.ibatis.session.SqlSession;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.Charset;

/**
 * @author 师文明
 * @data 2024/12/12 下午4:15
 **/
public class Test0 {
    static SqlSession sqlSession = SqlSessionUtil.openSession();
    static StockMapper mapper = sqlSession.getMapper(StockMapper.class);

    public static void main(String[] args) {
        try {
            for (int i = 138; i < 144; i++) {
                System.out.println("当前循环" + i);
                getData(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            sqlSession.commit();
            SqlSessionUtil.close(sqlSession);
        }
    }

    public static void getData(int page) throws Exception {
        //String url = "https://q.10jqka.com.cn/index/index/board/all/field/zdf/order/desc/page/"+page+"/ajax/1/";
        //深证
        String url = "https://q.10jqka.com.cn/index/index/board/ss/field/zdf/order/desc/page/" + page + "/ajax/1/";
        //上证
        //String url = "https://q.10jqka.com.cn/index/index/board/hs/field/zdf/order/desc/page/"+page+"/ajax/1/";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("Content-Type", "text/html; charset=gbk");
        con.setRequestProperty("Cookie", "Hm_lvt_722143063e4892925903024537075d0d=1734916752; HMACCOUNT=751B2B38DBE7BC37; Hm_lvt_78c58f01938e4d85eaf619eae71b4ed1=1734916753; Hm_lvt_929f8b362150b1f77b477230541dbbc2=1734916753; u_ukey=A10702B8689642C6BE607730E11E6E4A; u_uver=1.0.0; u_dpass=jCF9wdO0rX9kargLVl65oCJfALKbY9bW9vxQZTLoMRmAiEFp4OlF2dW7UAWZHZgaHi80LrSsTFH9a%2B6rtRvqGg%3D%3D; u_did=540A000AFB844843AE536E388320A970; u_ttype=WEB; ttype=WEB; user=MDpteF9sM3VkcTR6NDQ6Ok5vbmU6NTAwOjc2MjA0MDc5Nzo3LDExMTExMTExMTExLDQwOzQ0LDExLDQwOzYsMSw0MDs1LDEsNDA7MSwxMDEsNDA7MiwxLDQwOzMsMSw0MDs1LDEsNDA7OCwwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMSw0MDsxMDIsMSw0MDoxNjo6Ojc1MjA0MDc5NzoxNzM0OTMxNTE3Ojo6MTczMTY1OTcwMDo2MDQ4MDA6MDoxMmQ3YWQ3MzQ4ZGIyYWQ4MTY0MjhiMjIxNmMxNmY5YTI6ZGVmYXVsdF80OjA%3D; userid=752040797; u_name=mx_l3udq4z44; escapename=mx_l3udq4z44; ticket=878452f99e5b888e05ddbe26d6495218; user_status=0; utk=6405fb98ae7ddd7a11711b22bc8e16f1; Hm_lpvt_722143063e4892925903024537075d0d=1734939267; log=; Hm_lpvt_929f8b362150b1f77b477230541dbbc2=1734939268; Hm_lpvt_78c58f01938e4d85eaf619eae71b4ed1=1734939268; " +
                "v=AyE6ECNu4pk6pE5t5IM_kfOtMOY4zpXCv0I51IP2HSiH6k8Qyx6lkE-SSaIQ");

        // 从数据源获取实时股票数据
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), Charset.forName("GBK")));
        String inputLine;
        StringBuffer response = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }

        in.close();
        String data = response.toString();
        System.out.println(data);

        // 解析HTML内容
        Document document = Jsoup.parse(data, "UTF-8");
        String text = document.text();
        System.out.println(text);
        Element emElement = document.select("tbody").first();
        Elements tr = emElement.select("tr");

        for (Element element : tr) {
            Elements td = element.select("td");
            System.out.println(td.text());
            Stock stock = new Stock(null, td.get(1).text(), td.get(2).text(), 0);

            Stock stock1 = mapper.selectByCode(td.get(1).text());
            if (stock1 == null) {
                int count = mapper.insertStock(stock);
                System.out.println(count);
            } else {
                int count = mapper.updateStock(stock);
                System.out.println("已存在" + td.get(1).text());
            }

        }

    }
}
